package com.bjsxt.controller;

import com.bjsxt.pojo.Admin;
import com.bjsxt.service.AdminServer;
import com.bjsxt.utils.MD5Utils;
import com.bjsxt.utils.MapControl;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller

public class LoginController {

    @Autowired
    private AdminServer adminServer;

    @GetMapping("/login")
    public String v_login(){
        return "login";
    }


    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String account = map.get("account")+"";
        String password =map.get("password")+"";
        System.out.println(account+password);
        if(Strings.isNullOrEmpty(account)|| Strings.isNullOrEmpty(password)){
            return MapControl.getInstance().error("账号或密码不能为空").getMap();
        }

        Admin admin = adminServer.login(account, MD5Utils.getMD5(password));
        if (admin!=null) {
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);
            return MapControl.getInstance().success().getMap();
        }else {
            return MapControl.getInstance().error("账号或密码错误").getMap();

        }
    }

}
